import requests

from api.base import BaseApi
from libs.parser import parser_files, write_files

'''存token'''
class Login(BaseApi):
    def __init__(self):
        self._env_data = parser_files('D:\yuancheng_cangku\geren_daima\env\\test.yaml')
        '''平台相关数据'''
        self._pingtai_data = self._env_data['pingtai1']
        self.pingtai_BASE_URL = self._pingtai_data['host']
        self.pingtai_token = self.pingtai1_token()

        '''用户相关数据'''

        self._yonghu_data = self._env_data['yonghu1']
        self.yonghu_BASE_URL = self._yonghu_data['host']
        self.yonghu_token = self.yonghu1_token()

        '''商户相关数据'''
        self._shanghu_data = self._env_data['shanghu1']
        self.shanghu_BASE_URL = self._shanghu_data['host']
        self.shanghu_token = self.shanghu1_token()

        token_data = {
                'pingtai_token' : self.pingtai_token,
                'yonghu_token' : self.yonghu_token,
                'shanghu_token' : self.shanghu_token
        }

        write_files('../env/token.yaml',token_data)
        # self.user_BASE_URL = ''
        # self.shanghu_BASE_URL = ''

        # self.yonghu_token = self.yonghu1_token()
        # self.shanghu_token = self.shanghu1_token()




    def yonghu1_token(self):
        data = {
            'method': 'post',
            'url':  '/api/front/login/mobile/password',
            'headers' : {'content-type': 'application/json',},
            'json' : {
                'phone': self._yonghu_data['username'],
                'password': self._yonghu_data['password'],
                'spreadPid': 0,
            }
            }
        r = self.yonghu_qingqiu(data)
        return r['data']['token']

    def pingtai1_token(self):
        data = {
            'method': 'post',
            'url':  '/api/admin/platform/login',
            'headers': {'content-type': 'application/json', },
            'json': {
                'account': self._pingtai_data['username'],
                'pwd': self._pingtai_data['password'],
                'captchaVO': {},
            }
        }
        # r = requests.request(**data)
        r = self.pingtai_qingqiu(data)
        return r['data']['token']


    def shanghu1_token(self):
        data = {
            'method': 'post',
            'url': '/api/admin/merchant/login',
            'headers' : {'content-type': 'application/json',},
            'json' : {
                'account': self._shanghu_data['username'],
                'pwd': self._shanghu_data['password'],
                'captchaVO': {},
                'captcha': {'captchaVerification': '','secretKey': '','token': '',},
             }
        }
        r = self.shanghu_qingqiu(data)
        return r['data']['token']



if __name__ == '__main__':
    # print(Login().shanghu1_token())
    Login()
